package com.huonan.component.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelUtilTest {

    public static void main(String[] args) {
        extracted();
//        String expressFile = "/Users/lxp/IdeaProjects/Study/MainTest/src/main/java/com/huonan/component/excel/express.txt";
//        String text = "[吃瓜]";
//
//        List<String> expressList = FileUtil.readUtf8Lines(expressFile);
//        String newLine = text;
//        for (String e : expressList) {
//            newLine = newLine.replace(e, "");
//
//        }
//        System.out.println(newLine);
    }

    private static void extracted() {
        String expressFile = "/Users/lxp/IdeaProjects/Study/MainTest/src/main/java/com/huonan/component/excel/express.txt";
        String excelFile = "/Users/lxp/IdeaProjects/Study/MainTest/src/main/java/com/huonan/component/excel/input_excel.xlsx";

        String outFile = "/Users/lxp/IdeaProjects/Study/MainTest/src/main/java/com/huonan/component/excel/result4.xlsx";
//
        ExcelReader reader = ExcelUtil.getReader(excelFile);
        //List<List<User>> fileList = reader.read(1);
        List<User> read = reader.read(0, 1, 60000, User.class);
        List<String> dataList = new ArrayList<>();
        for (User user : read) {
            try {
                dataList.add(user.getName12());
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        List<String> newDataList = new ArrayList<>();
        List<String> expressList = FileUtil.readUtf8Lines(expressFile);
        for (String line : dataList) {
            String newLine = line;
            if (StringUtils.isNotEmpty(line)) {
                for (String e : expressList) {
                    try {
                        newLine = newLine.replace(e, "");
                        newLine = newLine.replaceAll("@\\S+($|\\s)", "");
                    } catch (Exception exception) {
                        System.out.println(e);
                    }
                }
            }else {
                newLine=" ";
            }
            newDataList.add(newLine);
        }
        ExcelWriter writer = ExcelUtil.getWriter(outFile);
        // 写入数据
//        writer.write(newDataList);
        // 批量写入数据
        for (int i = 0; i < newDataList.size(); i++) {
            writer.writeCellValue(0, i, newDataList.get(i));
        }
        writer.close();
        //  FileUtil.appendUtf8Lines(newDataList, outFile);
    }


//    private static List<User> getUserList() {
//        List<User> userList = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            userList.add(new User("张三", i));
//            userList.add(new User("李四", i));
//            userList.add(new User("王五", i));
//        }
//        BeanUtil.isEmpty(new User("王五", 30));
//        return userList;
//    }

    private static List<Object> customExpressionFromExcel(String file) {
        ExcelReader reader = ExcelUtil.getReader(file);
        List<List<Object>> fileList = reader.read(1);
        List<Object> expressionList = new ArrayList<>();
        fileList.forEach(l -> {
            expressionList.add(l.get(1));
            expressionList.add(l.get(2));
        });
        return expressionList;
    }

    private static List<Object> customExpressionFromExcel2(String file) {
        ExcelReader reader = ExcelUtil.getReader(file);
        List<List<Object>> fileList = reader.read(1);
        List<Object> expressionList = new ArrayList<>();
        fileList.forEach(l -> {
            expressionList.add(l.get(1));
            expressionList.add(l.get(2));
        });
        return expressionList;
    }

}
